package com.tcc.qa.test.util;

import org.apache.xpath.operations.String;
import org.openqa.jetty.html.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

import tcc.utils.None;

public class SelectFinder {
  public static Select getSelectById(WebDriver driver, String id) {
    return new Select(driver.findElement(By.id(id)));
  }

  public static Select getSelectByName(WebDriver driver, String name) {
    return new Select(driver.findElement(By.name(name)));
  }

  public static boolean isEmpty(WebDriver driver, String id) {
    return None.isEmpty(getSelectById(driver, id).getOptions());

  }

  public static WebElement getSelectOption(WebDriver driver, String id, int index) {
    return getSelectById(driver, id).getOptions().get(index);
  }

  public static WebElement getSelectOption(WebDriver driver, String id, String value) {
    List<WebElement> options = getSelectById(driver, id).getOptions();
    for (WebElement e : options) {
      if (value.equals(e.getText())) {
        return e;
      } else if (value.equals(e.getAttribute("value"))) {
        return e;
      } else if (value.equals(e.getAttribute("title"))) {
        return e;
      } else if (value.equals(e.getAttribute("name"))) { return e; }

    }
    return null;
  }
  /*
  public static WebElement getSelectOptionbyName(WebDriver driver, String name, String value) {
    List<WebElement> options = getSelectById(driver, name).getOptions();
    for (WebElement e : options) {
      if (value.equals(e.getText())) {
        return e;
      } else if (value.equals(e.getAttribute("value"))) {
        return e;
      } else if (value.equals(e.getAttribute("title"))) {
        return e;
      } else if (value.equals(e.getAttribute("name"))) { return e; }

    }
    return null;
  }
  */
  
  public static String getSelectText(WebDriver driver, String id, int index) {
    return getSelectById(driver, id).getOptions().get(index).getText();
  }
}
